﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GSLMS.Services.Interface;
using GSLMS.Entity;
using GSLMS.Core;
using GSLMS.Entity.DTO;

namespace GSLMS.Services
{
    public class OperationLogService :IOperationLog
    {



        public List<OperationLogDTO> GetAll(UniversallySearchParam searchparma)
        {

            List<OperationLogDTO> oplogs = new List<OperationLogDTO>();
            var sDate = searchparma.StarDateTime.Value;
            var eDate = searchparma.EndDateTime.Value;

            while (sDate <= eDate)
            {
                string OplogxmlfilePath = string.Format("{0}/{1}.xml", AppDomain.CurrentDomain.BaseDirectory + "~/OpLog", sDate.ToString("yyyyMMdd"));
                var tOpLogs = XmlUtil.Deserialize<List<OperationLogDTO>>(OplogxmlfilePath);
                oplogs.AddRange(tOpLogs);
                sDate = sDate.AddDays(1);
            }
            var query = oplogs.Where(c => 1 == 1) as IQueryable<OperationLogDTO>;
            if (!string.IsNullOrEmpty(searchparma.Ttile))
            {
                oplogs = oplogs.Where(c => c.ModuleName.IndexOf(searchparma.Ttile) >= 0).ToList();
            }
            if (!string.IsNullOrEmpty(searchparma.Creator))
            {
                oplogs = oplogs.Where(c => c.Operater.IndexOf(searchparma.Creator) >= 0).ToList();
            }
            if (!string.IsNullOrEmpty(searchparma.Updater))
            {
                oplogs = oplogs.Where(c => c.Detail.IndexOf(searchparma.Updater) >= 0).ToList();
            }
            var paginatonDTO = searchparma.Pagination;
            if (paginatonDTO.ReCount)
            {
                paginatonDTO.RowCount = oplogs.Count();
            }
            if (paginatonDTO.IsPaging)
            {
                oplogs = oplogs.OrderByDescending(c => c.OpTime).Skip(paginatonDTO.PageSize * (paginatonDTO.CurrentIndex - 1)).Take(paginatonDTO.PageSize).ToList();
            }
            return oplogs;

        }



    }
}
